import axios from '@/utils/request'

// const filterUserRemoteMethod = (query, context) => {
// axios
//   .get(`/manage/users`, { params: {
//     '@filter': `
//       entity.getUsername() == "${query}"
//       || entity.getId() == "${query}"
//     `
//   }})
//   .then(res => {
//     context.data = res.data.map(v => {
//       return { value: v.id, label: `${v.id}: ${v.__toString}` }
//     })
//   })
// }

const TYPE = {
  '-0-': '--- 普通 ---',
  'deposit': '收款',
  'burn-to-destroy': '提现',
  'deposit-fee': '支付费率',
  'deposit-from-mint': '提现失败返回',
  'order-settle-user': '订单分账'
}

export default {
  UserTransaction: {
    entity: { name: 'Transaction', prefix: () => `api` },
    list: {
      export: {
        query: {
          '@display': `{
          'id' : 'entity.getId()',
          'hash' : 'entity.getHash()',
          'status' : '{ "failed": "失败", "pending": "等待", "success": "成功" }[entity.getStatus()]',
          'type' : '${JSON.stringify(TYPE)}[entity.getType()]',
          'amount' : 'entity.getAmount()',
          'from' : 'entity.getType() == "deposit" || entity.getType() == "deposit-from-mint" ? "收入" : "支出"',
          'createdTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")'
        }`,
          limit: 10000000
        },
        label: {
          'id': 'id',
          'hash': '哈希',
          'status': '状态',
          'type': '类型',
          'amount': '金额',
          'from': '收入/支出',
          'createdTime': '创建时间'
        }
      },
      disabled_actions: ['new', 'lines'],
      query: {
        '@order': 'entity.id | DESC'
      },
      list_filter: {
        id: 'ID',
        hash: '哈希',
        type: Object.assign({ __label: '类型' }, TYPE),
        status: {
          '__label': '状态',
          'failed': '失败',
          'pending': '等待',
          'success': '成功'
        },
        startTime: {
          expression: `
          entity.getCreatedTime() >= datetime.get(":value")
        `,
          label: '开始时间',
          type: 'datetime'
        },
        endTime: {
          expression: `
          entity.getCreatedTime() < datetime.get(":value")
        `,
          label: '结束时间',
          type: 'datetime'
        }
      },
      list_display: [
        'id',
        { property: 'hash', label: '哈希' },
        {
          property: 'status', label: '状态',
          component: {
            methods: {
              filter: (type, status) => [
                { value: 'failed', label: '失败', tagType: 'danger' },
                { value: 'pending', label: '等待', tagType: 'warning' },
                { value: 'success', label: '成功', tagType: 'success' }
              ].find(obj => obj.value === status)[type]
            },
            props: ['data'],
            render(h) {
              return (
                <el-tag type={this.filter('tagType', this.data)}>
                  {this.filter('label', this.data)}
                </el-tag>
              )
            }
          }
        },
        {
          property: 'type', label: '类型',
          component: {
            props: ['data'],
            render(h) {
              return <span>{TYPE[this.data]}</span>
            }
          }
        },
        {
          property: 'amount', label: '金额',
          component: {
            props: ['data'],
            render(h) {
              return <span>￥{this.data}</span>
            }
          }
        },
        {
          property: 'from',
          label: '支出/收入',
          component: {
            props: ['data'],
            data() {
              return {
                type: ''
              }
            },
            created() {
              this.axios.get(`/api/user`).then((res) => {
                console.log('user', res.data)
                if (res.data.id === this.data.id) {
                  this.type = '支出'
                } else {
                  this.type = '收入'
                }
              })
            },
            render(h) {
              return (
                <div>{this.type}</div>
              )
            }
          }
        },
        // 'comment',
        // {
        //   property: 'attributes', label: '属性',
        //   component: {
        //     props: ['data'],
        //     data() {
        //       return {
        //         objects: Object.keys(this.data).map(
        //           k => { return { key: k, value: this.data[k] } }
        //         )
        //       }
        //     },
        //     render(h) {
        //       return (
        //         <el-popover
        //           width='300'
        //           placement='right'
        //           trigger='hover'
        //         >
        //           <el-table data={this.objects}>
        //             <el-table-column
        //               label='键'
        //               prop='key'
        //             />
        //             <el-table-column
        //               label='值'
        //               prop='value'
        //             />
        //           </el-table>
        //           <el-button slot='reference' size='small'>查看</el-button>
        //         </el-popover>
        //       )
        //     }
        //   }
        // },
        'createdTime'
      ]
    },
    form: {
      fields: [
        'id'
      ]
    }
  }
}
